#include "mode.h"
#include "lvgl/lvgl.h"
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <stdbool.h>
#include <pthread.h>
#include "animation.h"
#include "indicators.h"
#include "gear.h"
#include "ui/ui.h"
#include "tires.h"

void update_ui_for_mode(DrivingMode mode);

// 模式名称定义
const char *mode_names[] = {
    "普通模式", 
    "环保模式", 
    "竞速模式", 
    "夜间模式", 
    "智能模式"
};

// 当前驾驶模式(默认为普通模式)
DrivingMode current_driving_mode = MODE_NORMAL;


// 初始化驾驶模式
void init_driving_modes() {
    current_driving_mode = MODE_NORMAL;
    if (ui_Roller1) {
        lv_roller_set_selected(ui_Roller1, MODE_NORMAL, LV_ANIM_OFF);
    }
}

// 切换驾驶模式
void change_driving_mode(DrivingMode new_mode) {
    if (new_mode < MODE_NORMAL || new_mode > MODE_SMART) {
        printf("无效的驾驶模式: %d\n", new_mode);
        return;
    }
    
    current_driving_mode = new_mode;
    printf("已切换至: %s\n", mode_names[new_mode]);
    
    // 更新Roller选择状态
    if (ui_Roller1) {
        lv_roller_set_selected(ui_Roller1, new_mode, LV_ANIM_ON);
    }

}

// 获取当前模式名称
const char *get_current_mode_name() {
    return mode_names[current_driving_mode];
}

// Roller事件处理函数
static void roller_event_handler(lv_event_t* e) {
    lv_obj_t* roller = lv_event_get_target(e);
    uint16_t selected = lv_roller_get_selected(roller);
    change_driving_mode((DrivingMode)selected);
}

// 设置模式选择Roller
void setup_mode_roller() {
    if (!ui_Roller1) return;

    // 设置Roller选项
    lv_roller_set_options(ui_Roller1,
        "普通模式\n"
        "环保模式\n"
        "竞速模式\n"
        "夜间模式\n"
        "智能模式",
        LV_ROLLER_MODE_NORMAL
    );

    // 设置默认选中当前模式
    lv_roller_set_selected(ui_Roller1, current_driving_mode, LV_ANIM_OFF);

    // 添加事件回调
    lv_obj_add_event_cb(ui_Roller1, roller_event_handler, LV_EVENT_VALUE_CHANGED, NULL);
}


